package com.ithuangyl.common.widget.topbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.ithuangyl.common.R;
import com.ithuangyl.common.widget.topbar.callback.ILeftIcon;
import com.ithuangyl.common.widget.topbar.callback.ILeftTitle;
import com.ithuangyl.common.widget.topbar.callback.IRightIcon;
import com.ithuangyl.common.widget.topbar.callback.IRightTitle;
import com.ithuangyl.common.widget.topbar.callback.ITitle;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/19 14:43
 */
public class TopBar extends Toolbar implements View.OnClickListener {


    private Context context;
    AppCompatTextView vLeftTitle;
    AppCompatTextView vTitle;
    AppCompatTextView vRightTile;

    AppCompatImageView vLeftIcon;
    AppCompatImageView vRightIcon;

    ILeftTitle iLeftTitle;
    ITitle iTitle;
    IRightTitle iRightTitle;
    ILeftIcon iLeftIcon;
    IRightIcon iRightIcon;

    public TopBar(Context context) {
        this(context, null, 0);
    }

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {

        LayoutInflater.from(context).inflate(R.layout.view_top_bar, this);
        vLeftTitle = $(R.id.id_left_title);
        vTitle = $(R.id.id_title);
        vRightTile = $(R.id.id_right_title);
        vLeftIcon = $(R.id.id_left_icon);
        vRightIcon = $(R.id.id_right_icon);

        vLeftTitle.setOnClickListener(this);
        vTitle.setOnClickListener(this);
        vRightTile.setOnClickListener(this);
        vLeftIcon.setOnClickListener(this);
        vRightIcon.setOnClickListener(this);
    }

    private <T extends View> T $(int id) {
        return findViewById(id);
    }

    public TopBar leftTitle(String title, ILeftTitle iLeftTitle) {
        return leftTitle(title, R.color.black, iLeftTitle);
    }

    public TopBar leftTitle(String title, int textColor, ILeftTitle iLeftTitle) {
        if (vLeftTitle != null) {
            vLeftTitle.setText(title);
            vLeftTitle.setTextColor(ContextCompat.getColor(context, textColor));
            this.iLeftTitle = iLeftTitle;
        }
        return this;
    }

    public TopBar title(String title, ITitle iTitle) {
        return title(title, R.color.black, iTitle);
    }

    public TopBar title(String title, int textColor, ITitle iTitle) {
        if (vTitle != null) {
            vTitle.setText(title);
            vTitle.setTextColor(ContextCompat.getColor(context, textColor));
            this.iTitle = iTitle;
        }
        return this;
    }

    public TopBar rightTitle(String title, IRightTitle iRightTitle) {

        return rightTitle(title, R.color.black, iRightTitle);
    }

    public TopBar rightTitle(String title, int textColor, IRightTitle iRightTitle) {
        if (vRightTile != null) {
            vRightTile.setText(title);
            this.iRightTitle = iRightTitle;
        }
        return this;
    }

    public TopBar leftIcon(int resId, ILeftIcon iLeftIcon) {
        if (vLeftIcon != null) {
            vLeftIcon.setImageResource(resId);
            this.iLeftIcon = iLeftIcon;
        }
        return this;
    }

    public TopBar rightIcon(int resId, IRightIcon iRightIcon) {
        if (vRightIcon != null) {
            vRightIcon.setImageResource(resId);
            this.iRightIcon = iRightIcon;
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v == vLeftTitle) {
            if (iLeftTitle != null) {
                iLeftTitle.onLeftTitle(vLeftTitle);
            }
        } else if (v == vTitle) {
            if (iTitle != null) {
                iTitle.onTitle(vTitle);
            }
        } else if (v == vRightTile) {
            if (iRightTitle != null) {
                iRightTitle.onRightTitle(vRightTile);
            }
        } else if (v == vLeftIcon) {
            if (iLeftIcon != null) {
                iLeftIcon.onLeftIcon(vLeftIcon);
            }
        } else if (v == vRightIcon) {
            if (iRightIcon != null) {
                iRightIcon.onRightIcon(vRightIcon);
            }
        }
    }

    public TopBar showLeftTitle(boolean isShow) {
        if (vLeftTitle != null) {
            vLeftTitle.setVisibility(isShow ? VISIBLE : GONE);
        }
        return this;
    }

    public TopBar showTitle(boolean isShow) {
        if (vTitle != null) {
            vTitle.setVisibility(isShow ? VISIBLE : GONE);
        }
        return this;
    }

    public TopBar showRightTitle(boolean isShow) {
        if (vRightTile != null) {
            vRightTile.setVisibility(isShow ? VISIBLE : GONE);
        }
        return this;
    }

    public TopBar showLeftIcon(boolean isShow) {
        if (vLeftIcon != null) {
            vLeftIcon.setVisibility(isShow ? VISIBLE : GONE);
        }
        return this;
    }
    public TopBar setLeftIconColor(int colorId){
        if (vLeftIcon!=null){
            vLeftIcon.setColorFilter(ContextCompat.getColor(context,colorId));
        }
        return  this;
    }

    public TopBar showRightIcon(boolean isShow) {
        if (vRightIcon != null) {
            vRightIcon.setVisibility(isShow ? VISIBLE : GONE);
        }
        return this;
    }
}
